package com.zr.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.User;
import com.zr.service.FunctionService;
import com.zr.service.UserService;

import net.sf.json.JSONArray;

@Controller
@Scope(value="prototype")
public class LoginAction extends  ActionSupport  implements  ServletResponseAware,SessionAware{
	/**
	 * @author CY
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	private Map<String, Object> session;
	
	private  HttpServletResponse  resp;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Resource
	private  UserService  userService;
	
	@Resource
	private FunctionService functionservice;
	
	public String login(){
		User  currentuser =userService.findUserByUserNameAndUserPsw(user);
		if(null!=currentuser){
			  session.put("user", currentuser);
			  return "success";
		  }else{
			return "error";
			}
	}
	
	public void findFuncsByRole(){
		resp.setCharacterEncoding("utf8");
		User  currentuser = (User)session.get("user");
		JSONArray array = functionservice.findUserFunctionByRole(currentuser.getRole().getRid());
		try {
			resp.getWriter().write(array.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.resp = response;
		
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session=session;
		
	}
	

	
}
